<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

defined('IN_ADMIN') or die('Hacking Attempt!');

func::isLogin();
mysql::begin(1);

$sTable = 'tnf.order';
$iTotal = 0;

$iOrderId = (isset($_GET['id']) && is_numeric($_GET['id'])) ? $_GET['id'] : 0;

if(isset($_POST['updateOrderButton'])){
    $aTotal = isset($_POST['val']) ? func::replaceNumberFormat($_POST['val']) : array(array());

    for($i=0; $i < (int)((count($aTotal, 1) - 3) / 3); $i++){
        //prepare date
        $iQuantity = is_numeric($aTotal['quantity'][$i]) ? $aTotal['quantity'][$i] : 0;
        $iPrice = is_numeric($aTotal['price'][$i]) ? $aTotal['price'][$i] : 0;

        //calculate total amount
        $iTotal += $iQuantity * $iPrice;
    }

   $aVals = array(
        'customerId' => isset($_POST['customerId']) ? $_POST['customerId'] : 0,        
        'total' => $iTotal,
        'content' => serialize($aTotal),
        'delivered' => isset($_POST['delivered']) ? $_POST['delivered'] : 0,
        'date' => time(),
        'username' => $_SESSION[SESSION_NAME]
   );
   $iIdUpdateOrder = mysql::updateSql($sTable, $aVals, 'orderId', $iOrderId);
   
   //update debit amount
   $iOrderOldTotal = isset($_POST['orderOldTotal']) ? $_POST['orderOldTotal'] : 0;
   $iDebitAmount = mysql::selectDebitAmount('debit', $aVals['customerId']);
   $iTotal = $iDebitAmount['amount'] + ($iTotal - $iOrderOldTotal);
   $iIdUpdateDebit = mysql::updateSql('debit', array('amount' => $iTotal), 'customerId', $aVals['customerId']);

   //update inventory
   $aOrderOldContent = isset($_POST['orderOldContent']) ? unserialize(str_replace(SESSION_NAME, '"', $_POST['orderOldContent'])) : array(array());
   for($i=0; $i < (int)((count($aOrderOldContent, 1)-3)/3); $i++){
       $iIdInvent = explode('.', $aOrderOldContent['inventoryId'][$i]);
       $aInventory['inventoryId'][$i] = $iIdInvent[0];
   }
   foreach($aInventory['inventoryId'] as $key=>$value){
       $aRow = mysql::selectInventoryQuantity('inventory', $value);
       $iInventoryQuantity = $aRow['quantity'] - ($aTotal['quantity'][$key] - $aOrderOldContent['quantity'][$key]);
       if($iInventoryQuantity > 0)
            $iIdUpdateInventory = mysql::updateSql('inventory', array('quantity' => $iInventoryQuantity), 'inventoryId', $value);
       else
           die($aRow['title'].' không còn đủ hàng để bán.');
   }
   
   if($iIdUpdateOrder && $iIdUpdateDebit && $iIdUpdateInventory){       
       mysql::commit();
       func::redirect (func::makeUrl ('order', 'list'));
   }else
       mysql::rollBack();
}else{
    //order
    $sSql = 'SELECT * FROM '. $sTable .' WHERE orderId='. $iOrderId;
    $aResult = mysql::querySql($sSql);
    $aRow = mysql::fetchSql($aResult);
    $aVals = unserialize($aRow['content']);

    //customer
    $sSql = 'SELECT name FROM customer WHERE customerId='. $aRow['customerId'];
    $aResult = mysql::querySql($sSql);
    $aCustomerRow = mysql::fetchSql($aResult);   
}
?>
